<template>
  <div class="mod-user">
    <el-form class="policyForm">
      <!-- <el-form-item>
        <el-input v-model="dataForm.userName" placeholder="用户名" clearable></el-input>
      </el-form-item> -->
      <el-form-item>
        <el-input :clearable="true" class="inputcss" v-model="input" placeholder="请输文章标题查询"></el-input>
        <el-button @click="clickInquire()">查询</el-button>
        <el-button @click="clickGetData()">重置</el-button>
        <el-button type="primary" plain v-if="isAuth('sys:policy:save')" @click="addOrUpdateHandle()">新增</el-button>
        <!-- <el-button type="danger" @click="deleteHandle()">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
       border
      style="width: 100%">
      <el-table-column
        prop="author"
        label="作者名称"
        align="center"
        min-width="100"
        >
      </el-table-column>
      <el-table-column
        prop="title"
        label="政策文章标题"
        align="center"
        :show-overflow-tooltip="true"
        min-width="400">
      </el-table-column>
      <el-table-column
        prop="url"
        label="政策文章路径"
        align="center"
        :show-overflow-tooltip="true"
        min-width="400">
      </el-table-column>
      <el-table-column
        prop="source"
        align="center"
        min-width="150"
        label="文章来源">
      </el-table-column>
      <el-table-column
        prop="visible"
        align="center"
        label="状态"
        min-width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.visible === 0">
            不可见
          </span>
          <span v-else-if="scope.row.visible === 1">
            可见
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        align="center"
        label="操作"
        min-width="160">
        <template slot-scope="scope">
        <!-- <el-button style="color:#53b4ff;" @click="handleClick(scope.row)" type="text" size="small">查看</el-button> -->
        <el-button style="color:#53b4ff;" @click="handleClickModification(scope.row.id)" type="text" size="small">修改</el-button>
        <el-button style="color:red;" @click="handleClickDelete(scope.row.id)" type="text" size="small">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 10, 20, 50, 100, 200]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- <policy-type v-if="policyCenterDialogVisible"></policy-type> -->
    <!-- 新增 / 修改 -->
  <el-dialog
  v-if="policyCenterDialogVisible"
  title="提示"
  :visible.sync="policyCenterDialogVisible"
  :close-on-click-modal="false"
  width="60%"
  center>
  <span>
    <!-- 表单 -->
  <el-form :model="ruleForm" :rules="ruleRule" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="文章标题" prop="title">
    <el-input v-model="ruleForm.title"></el-input>
  </el-form-item>
  <el-form-item label="文章路径" prop="url">
    <el-input v-model="ruleForm.url"></el-input>
  </el-form-item>
  <el-form-item label="作者名称" prop="author">
    <el-input v-model="ruleForm.author"></el-input>
  </el-form-item>
  <el-form-item label="文章来源" prop="source">
    <el-input v-model="ruleForm.source"></el-input>
  </el-form-item>
  <el-form-item label="排序" prop="sort">
    <el-input-number v-model="sort" controls-position="right" @change="handleChange" :min="1" :max="1000"></el-input-number>
  </el-form-item>
  <el-form-item label="是否可见">
  <el-radio v-model="radio" :label="1">可见</el-radio>
  <el-radio v-model="radio" :label="0">不可见</el-radio>
  </el-form-item>
</el-form>
  </span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="clickFalseLink">取 消</el-button>
    <el-button type="primary" plain @click="clickNewlyLink()">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
// import PolicyType from './policy-type'
import Vue from 'vue'
export default {
  data () {
    return {
      id: 0,
      isEdit: false,
    policyCenterDialogVisible:false,
    ruleForm:{
      id:0,
      title:'',
      url:'',
      author:'',
      source:''
    },
    sort:1,
    radio:1,
    tableData: [],
        ruleRule:{
        title: [
          { required: true, message: '请填写文章标题', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请填写文章路径', trigger: 'blur' }
        ]
        },
    input:'',
  // 当前页数
  pageIndex:1,
  // 每页条数
  pageSize:10,
  totalCount:0,
  // isPics: true,
    }
  },
  components:{
  // PolicyType
  },
  created(){
  this.getPolicyList()
  },
  watch: {
    policyCenterDialogVisible: {
      handler(val) {
        if(!val) {
          this.resetForm()
          this.radio = 1
          this.sort = 1
        }
      }
    }
  },
  methods:{
  addOrUpdateHandle(){
  this.policyCenterDialogVisible = true
  this.isEdit = false
  },
  handleChange(){
  console.log(123)
  },
  clickFalseLink(){
  this.policyCenterDialogVisible = false
  this.$refs.ruleForm.resetFields()
  },
  // 重置
  clickGetData(){
  this.input = '',
  this.clickInquire()
  },
  // 查询
  clickInquire(){
  this.$http({
              url: this.$http.adornUrl(`/sys/policy/list`),
              method: 'get',
              params: this.$http.adornParams({
              //  'visible':this.radio,
               'page':this.pageIndex,
               'limit':this.pageSize,
               'title':this.input
              })
            }).then(({data}) => {
              if (data.code === 0) {
              this.tableData = data.page.list
              this.totalCount = data.page.totalCount
              this.pageIndex = data.page.currPage
              this.pageSize = data.page.pageSize
              } else {
                this.$message.error(data.msg)
              }
            })
  },
  //新增的确定
  clickNewlyLink(){
    if(!this.isEdit){
      this.$refs['ruleForm'].validate((valid) => {
          if(valid){
           this.$http({
              url: this.$http.adornUrl(`/sys/policy/save/`),
              method: 'post',
              params: this.$http.adornParams({
                'title':this.ruleForm.title,
                'url':this.ruleForm.url,
                'author':this.ruleForm.author,
                'source':this.ruleForm.source,
                'sort':this.sort,
                'visible':this.radio,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '上传成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.policyCenterDialogVisible = false
                    this.getPolicyList()
                    // this.$emit('refreshDataList')
                    this.$refs.ruleForm.resetFields()
                  }
                })
              this.$refs.ruleForm.resetField()
              } else {
                this.$message.error(data.msg)
                this.$refs.ruleForm.resetFields()
              }
            })
            }
      })
    } else {
          this.$refs['ruleForm'].validate((valid) => {
          if(valid){
           this.$http({
              url: this.$http.adornUrl(`/sys/policy/update/`),
              method: 'post',
              params: this.$http.adornParams({
                'id': this.id,
                'title':this.ruleForm.title,
                'url':this.ruleForm.url,
                'author':this.ruleForm.author,
                'source':this.ruleForm.source,
                'sort':this.sort,
                'visible':this.radio,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '上传成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.policyCenterDialogVisible = false
                    this.getPolicyList()
                    // this.$emit('refreshDataList')
                    this.$refs.ruleForm.resetFields()
                  }
                })
              this.$refs.ruleForm.resetField()
              } else {
                this.$message.error(data.msg)
                this.$refs.ruleForm.resetFields()
              }
            })
            }
       })
    }
      
  

  },
  // 获取数据
  getPolicyList(){
           this.$http({
              url: this.$http.adornUrl(`/sys/policy/list`),
              method: 'get',
              params: this.$http.adornParams({
               'page':this.pageIndex,
               'limit':this.pageSize
              })
            }).then(({data}) => {
              if (data.code === 0) {
              this.tableData = data.page.list
              this.totalCount = data.page.totalCount
              this.pageIndex = data.page.currPage
              this.pageSize = data.page.pageSize
              } else {
                this.$message.error(data.msg)
              }
            })
  },
  // 删除
  handleClickDelete(id){
  console.log(id);
  this.$http({
        url: this.$http.adornUrl(`/sys/policy/delete/`+id),
        method: 'get',
      }).then(({data}) => {
        if (data.code === 0) {
          this.$message.success('删除成功')
          this.getPolicyList()
        } else {
          this.$message.error(data.msg)
        }
      })
  },
  sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getPolicyList()
      },
  currentChangeHandle (val) {
     this.pageIndex = val
     // if(this.isPics){
       this.getPolicyList()
     // }else{
       // this.getNopicCert()
     // }
   },
   handleClickModification(id){
  //  console.log(id);
     this.policyCenterDialogVisible = true
     this.isEdit = true
     this.id = id
      this.$http({
        url: this.$http.adornUrl(`/sys/policy/getDetail/`+id),
        method: 'get',
      }).then(({data}) => {
        if (data.code === 0) {
        console.log(data.data.visible);
        this.ruleForm.title = data.data.title
        this.ruleForm.url = data.data.url
        this.ruleForm.author = data.data.author
        this.ruleForm.source = data.data.source
        this.sort = data.data.sort
        this.radio = data.data.visible
        this.ruleForm.id = data.data.id
        } else {
          this.$message.error(data.msg)
        }
      })
   },
   resetForm() {
      this.ruleForm = {
      title:'',
      url:'',
      author:'',
      source:''
      }
   }

}
}
</script>
<style scoped lang="scss">
  .inputcss{
  width: 300px;
  margin-right: 5px;
  }
  .policyForm{
    margin-top: 10px;
  }
  .mod-user{
    padding: 15px;
}
</style>
